using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    public partial class CustomerOrderForm : System.Web.UI.Page
    {
        public enum DisplayType
        {
            Edit, Add
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(GetDisplayType == DisplayType.Add)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                    
                        FillCustomer(db);
                    
                    }
                }            
                if (GetDisplayType == DisplayType.Edit)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                        CustomerOrder entity = db.CustomerOrders.Single(_ => _.Id == GetEditID);
                        CustomerOrderOrderNameInput.Text = entity.OrderName;
                        FillCustomer(db);
          CustomerIdInput.SelectedValue = entity.Customer.ToString()
        ;
                        CustomerOrderQuantityInput.Text = entity.Quantity.ToString("#,##0.#");
                        }
                }
            }
        }
        
        
        
          private void FillCustomer(DataClasses1DataContext db)
          {
              CustomerIdInput.DataSource = db.Customers;
              CustomerIdInput.DataTextField = "CustomerName";
              CustomerIdInput.DataValueField = "Id";
              CustomerIdInput.DataBind();
          }
        
        
        public DisplayType GetDisplayType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    return DisplayType.Edit;
                else
                    return DisplayType.Add;
            }
        }
        private int GetEditID
        {
            get { return int.Parse(Request.QueryString["ID"]); }
        }
        protected void CustomerOrderSaveButton_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                CustomerOrder entity;
                if (GetDisplayType == DisplayType.Edit)
                    entity = db.CustomerOrders.Single(_ => _.Id == GetEditID);
                else
                {
                    entity = new CustomerOrder();
                    db.CustomerOrders.InsertOnSubmit(entity);
                }
                entity.OrderName = CustomerOrderOrderNameInput.Text;
                entity.Customer = int.Parse(CustomerIdInput.SelectedValue);
                entity.Quantity = int.Parse(CustomerOrderQuantityInput.Text);;
                db.SubmitChanges();
            }
        }
        protected void CustomerOrderCancelButton_Click(object sender, EventArgs e)
        {

        }
    }
}